package com.yvtian.inv.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yvtian.inv.pojo.*;
import com.yvtian.inv.dao.InvMaterialRequestMapper;
import com.yvtian.inv.service.*;
import com.yvtian.utils.StringHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 生产领料单(inv_material_request)表服务实现类
 *
 * @author long
 * @since 2024-07-02 13:43:36
 */
@Service("invMaterialRequestService")
public class InvMaterialRequestServiceImpl extends ServiceImpl<InvMaterialRequestMapper, InvMaterialRequestEntity> implements InvMaterialRequestService {

    @Autowired
    private InvMaterialRequestItemService invMaterialRequestItemService;
    @Autowired
    private ShopService shopService;
    @Autowired
    private InvOrderShopService invOrderShopService;
    @Autowired
    private InvOrderService invOrderService;

    @Override
    public IPage<InvMaterialRequestEntity> findByPage(Page<InvMaterialRequestEntity> page, InvMaterialRequestEntity invMaterialRequestEntity) {
        LambdaQueryWrapper<InvMaterialRequestEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(InvMaterialRequestEntity::getIsDel, 0);
        getQueryWrapper(invMaterialRequestEntity, queryWrapper);
        return this.page(page, queryWrapper);
    }

    @Transactional
    @Override
    public boolean insertOrUpdateAll(InvMaterialRequestEntity invMaterialRequestEntity) {
        ArrayList<InvMaterialRequestItemEntity> invMaterialRequestItemList = invMaterialRequestEntity.getInvMaterialRequestItemList();
        if (invMaterialRequestItemList != null && !invMaterialRequestItemList.isEmpty()) {
            LambdaUpdateWrapper<InvMaterialRequestItemEntity> itemUpdateWrapper = new LambdaUpdateWrapper<>();
            itemUpdateWrapper.eq(InvMaterialRequestItemEntity::getZcode, invMaterialRequestEntity.getCode());
            invMaterialRequestItemService.remove(itemUpdateWrapper);
            invMaterialRequestItemService.saveBatch(invMaterialRequestItemList);
        }
        this.saveOrUpdate(invMaterialRequestEntity);
        return true;
    }

    @Transactional
    @Override
    public boolean handleStorage(InvMaterialRequestEntity invMaterialRequest) {
        List<InvMaterialRequestItemEntity> itemEntityList = invMaterialRequestItemService.list(new LambdaQueryWrapper<InvMaterialRequestItemEntity>()
                .eq(InvMaterialRequestItemEntity::getZcode, invMaterialRequest.getCode()));

        for (InvMaterialRequestItemEntity invMaterialRequestItemEntity : itemEntityList) {
            if (invMaterialRequestItemEntity.getReceiveCount() == null || invMaterialRequestItemEntity.getReceiveCount().compareTo(BigDecimal.ZERO) <= 0) {
                continue;
            }
            //更新商品库存
            LambdaUpdateWrapper<ShopEntity> shopEntityWrapper = new LambdaUpdateWrapper<>();
            shopEntityWrapper.setSql("quantity = quantity - " + invMaterialRequestItemEntity.getReceiveCount());
            shopEntityWrapper.eq(ShopEntity::getShopCode, invMaterialRequestItemEntity.getShopCode());
            shopService.update(shopEntityWrapper);

            //更新订单表详情的领取数量数据
            LambdaUpdateWrapper<InvOrderShopEntity> invOrderItemUpdateWrapper = new LambdaUpdateWrapper<>();
            invOrderItemUpdateWrapper.setSql("receive_count = receive_count + " + invMaterialRequestItemEntity.getReceiveCount());
            invOrderItemUpdateWrapper.eq(InvOrderShopEntity::getShopCode, invMaterialRequestItemEntity.getShopCode());
            invOrderItemUpdateWrapper.eq(InvOrderShopEntity::getZcode, invMaterialRequest.getInvOrderCode());
            invOrderShopService.update(invOrderItemUpdateWrapper);
        }

        //更新领料单领料状态
        LambdaQueryWrapper<InvOrderShopEntity> invOrderItemQueryWrapper = new LambdaQueryWrapper<>();
        invOrderItemQueryWrapper.eq(InvOrderShopEntity::getZcode, invMaterialRequest.getInvOrderCode());
        List<InvOrderShopEntity> invOrderShopList = invOrderShopService.list(invOrderItemQueryWrapper);
        int materialRequestStatus = 2;
        for (InvOrderShopEntity invOrderShopEntity : invOrderShopList) {
            if (invOrderShopEntity.getReceiveCount().compareTo(invOrderShopEntity.getRequiredQuantity()) < 0) {//领用数量<需求数量
                materialRequestStatus = 1;
                break;
            }
        }
        //更新生产订单表的领料状态
        invOrderService.update(new LambdaUpdateWrapper<InvOrderEntity>()
                .set(InvOrderEntity::getMaterialRequestStatus, materialRequestStatus)
                .eq(InvOrderEntity::getCode, invMaterialRequest.getInvOrderCode()));

        //更新主表状态，（审核）
        LambdaUpdateWrapper<InvMaterialRequestEntity> materialRequestUpdateWrapper = new LambdaUpdateWrapper<>();
        materialRequestUpdateWrapper.eq(InvMaterialRequestEntity::getCode, invMaterialRequest.getCode());
        materialRequestUpdateWrapper.set(InvMaterialRequestEntity::getStatus, 1);
        materialRequestUpdateWrapper.set(InvMaterialRequestEntity::getCheckName, invMaterialRequest.getCheckName());
        materialRequestUpdateWrapper.set(InvMaterialRequestEntity::getCheckUser, invMaterialRequest.getCheckUser());
        materialRequestUpdateWrapper.set(InvMaterialRequestEntity::getCheckTime, new Date());
        this.update(materialRequestUpdateWrapper);
        return true;
    }

    private void getQueryWrapper(InvMaterialRequestEntity invMaterialRequestEntity, LambdaQueryWrapper<InvMaterialRequestEntity> queryWrapper) {
        queryWrapper
                .like(StringHelper.isNotEmpty(invMaterialRequestEntity.getCode()), InvMaterialRequestEntity::getCode, invMaterialRequestEntity.getCode())
                .like(StringHelper.isNotEmpty(invMaterialRequestEntity.getInvOrderCode()), InvMaterialRequestEntity::getInvOrderCode, invMaterialRequestEntity.getInvOrderCode())
                .like(StringHelper.isNotEmpty(invMaterialRequestEntity.getStockCode()), InvMaterialRequestEntity::getStockCode, invMaterialRequestEntity.getStockCode())
                .like(StringHelper.isNotEmpty(invMaterialRequestEntity.getStockName()), InvMaterialRequestEntity::getStockName, invMaterialRequestEntity.getStockName())
                .eq(invMaterialRequestEntity.getReqQuantity() != null, InvMaterialRequestEntity::getReqQuantity, invMaterialRequestEntity.getReqQuantity())
                .eq(invMaterialRequestEntity.getAmount() != null, InvMaterialRequestEntity::getAmount, invMaterialRequestEntity.getAmount())
                .like(StringHelper.isNotEmpty(invMaterialRequestEntity.getProjectCode()), InvMaterialRequestEntity::getProjectCode, invMaterialRequestEntity.getProjectCode())
                .like(StringHelper.isNotEmpty(invMaterialRequestEntity.getProjectName()), InvMaterialRequestEntity::getProjectName, invMaterialRequestEntity.getProjectName())
                .like(StringHelper.isNotEmpty(invMaterialRequestEntity.getReqDeptCode()), InvMaterialRequestEntity::getReqDeptCode, invMaterialRequestEntity.getReqDeptCode())
                .like(StringHelper.isNotEmpty(invMaterialRequestEntity.getReqDeptName()), InvMaterialRequestEntity::getReqDeptName, invMaterialRequestEntity.getReqDeptName())
                .like(StringHelper.isNotEmpty(invMaterialRequestEntity.getShopCode()), InvMaterialRequestEntity::getShopCode, invMaterialRequestEntity.getShopCode())
                .like(StringHelper.isNotEmpty(invMaterialRequestEntity.getShopName()), InvMaterialRequestEntity::getShopName, invMaterialRequestEntity.getShopName())
                .eq(invMaterialRequestEntity.getUnit() != null, InvMaterialRequestEntity::getUnit, invMaterialRequestEntity.getUnit())
                .like(StringHelper.isNotEmpty(invMaterialRequestEntity.getRequestedUsername()), InvMaterialRequestEntity::getRequestedUsername, invMaterialRequestEntity.getRequestedUsername())
                .like(StringHelper.isNotEmpty(invMaterialRequestEntity.getRequestedAccount()), InvMaterialRequestEntity::getRequestedAccount, invMaterialRequestEntity.getRequestedAccount())
                .like(StringHelper.isNotEmpty(invMaterialRequestEntity.getIssuedUsername()), InvMaterialRequestEntity::getIssuedUsername, invMaterialRequestEntity.getIssuedUsername())
                .like(StringHelper.isNotEmpty(invMaterialRequestEntity.getIssuedAccount()), InvMaterialRequestEntity::getIssuedAccount, invMaterialRequestEntity.getIssuedAccount())
                .eq(invMaterialRequestEntity.getStatus() != null, InvMaterialRequestEntity::getStatus, invMaterialRequestEntity.getStatus())
                .like(StringHelper.isNotEmpty(invMaterialRequestEntity.getDelName()), InvMaterialRequestEntity::getDelName, invMaterialRequestEntity.getDelName())
                .like(StringHelper.isNotEmpty(invMaterialRequestEntity.getCreateName()), InvMaterialRequestEntity::getCreateName, invMaterialRequestEntity.getCreateName())
                .like(StringHelper.isNotEmpty(invMaterialRequestEntity.getCreateUser()), InvMaterialRequestEntity::getCreateUser, invMaterialRequestEntity.getCreateUser())
                .like(StringHelper.isNotEmpty(invMaterialRequestEntity.getUpdateName()), InvMaterialRequestEntity::getUpdateName, invMaterialRequestEntity.getUpdateName())
                .like(StringHelper.isNotEmpty(invMaterialRequestEntity.getUpdateUser()), InvMaterialRequestEntity::getUpdateUser, invMaterialRequestEntity.getUpdateUser())
                .like(StringHelper.isNotEmpty(invMaterialRequestEntity.getRemarks()), InvMaterialRequestEntity::getRemarks, invMaterialRequestEntity.getRemarks())
                .like(StringHelper.isNotEmpty(invMaterialRequestEntity.getCheckName()), InvMaterialRequestEntity::getCheckName, invMaterialRequestEntity.getCheckName())
                .like(StringHelper.isNotEmpty(invMaterialRequestEntity.getCheckUser()), InvMaterialRequestEntity::getCheckUser, invMaterialRequestEntity.getCheckUser())
                .like(invMaterialRequestEntity.getCheckTime() != null, InvMaterialRequestEntity::getCheckTime, invMaterialRequestEntity.getCheckTime());
    }
}

